package com.you.atlas.web.home.helper;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import com.you.common.file.ImageService;

import weibo4j.Account;
import weibo4j.Users;
import weibo4j.Weibo;
import weibo4j.model.User;
import weibo4j.model.WeiboException;
import weibo4j.org.json.JSONException;
import weibo4j.org.json.JSONObject;

public class SinaClientHelper {

    public static String getUid(String accessToken) throws WeiboException {
        Weibo weibo = new Weibo();
        weibo.setToken(accessToken);
        Account am = new Account();
        JSONObject json = am.getUid();
        try {
            return json.getString("uid");
        } catch (JSONException e) {
            return null;
        }
    }

    public static User getUser(String accessToken, String uid) throws WeiboException {
        Weibo weibo = new Weibo();
        weibo.setToken(accessToken);
        Users um = new Users();
        User user = um.showUserById(uid);
        return user;
    }

    public static String getFaceThubmUrl(ImageService imageService, String imageURL) {
        imageURL = imageURL.replace("/50/", "/180/");
        try {
            return imageService.scaleImg(new URL(imageURL).openStream(), imageURL + ".jpg", 115, 130);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getFaceIconUrl(ImageService imageService, String imageURL) {
        if (imageURL == null) {
            return null;
        }
        return imageService.scaleIcon(imageURL, 48, 48);
    }
}
